/*
************************************************************************************************************************
	Creates estimation dataset $workdata/parents with information on:
	- Social assistance (sa)
	- Employment (wage)
	- Earnings (wage2)
	- Cohabiting status with children (parent=1 if living with child)

	Uses auxiliary datasets:
	- $temp/baseline_2_`year' (see "$data_do/baseline")

	Registers: Variables (Years) 
	- BEF: pnr, familie_id, alder, koen (2000-2006)

************************************************************************************************************************
*/

*********************************************************************
* Genereating cohabitant status of children for fathers and mothers.
* Save bef_far`year' (fathers), bef_mor`year' (mothers)
*********************************************************************

forvalues year=2000/2006 {
use "$rawdata\bef`year'", clear
keep if koen==1
drop far_id
clonevar far_id=pnr
clonevar familie_id_far=familie_id`year'
keep pnr far_id familie_id_far
merge 1:m far_id using "$rawdata\bef`year'", keep(master match) keepusing(familie_id`year' alder`year')
sort pnr
g cohab_far=familie_id_far==familie_id`year' & alder`year'<18
collapse (sum) cohab_far, by(pnr)
g year=`year'
rename cohab_far cohab_far`year'
save "$temp/bef_far`year'", replace

use "$rawdata\bef`year'", clear
keep if koen==2
drop mor_id
clonevar mor_id=pnr
clonevar familie_id_mor=familie_id`year'
keep pnr mor_id familie_id_mor
merge 1:m mor_id using "$rawdata\bef`year'", keep(master match) keepusing(familie_id`year' alder`year')
sort pnr
g cohab_mor=familie_id_mor==familie_id`year' & alder`year'<18
collapse (sum) cohab_mor, by(pnr)
g year=`year'
rename cohab_mor cohab_mor`year'
save "$temp/bef_mor`year'", replace
}

********************************************
* Use baseline sample before applying selection criteria.
********************************************

forvalues year = 2000/2006 {
use "$temp/baseline_2_`year'", clear  /* see $data_do/baseline.do */

********************************************
* Applying selelction criteria of Baseline selection except for parental status
********************************************

keep if ie_type==1
keep if edul=="10" 
keep if eduf=="10" 
keep if fm_markl==6
keep if fm_markf==6 
*keep if d_childl==1 
*keep if d_childf==1 
keep if civstl=="U" 
keep if civstf=="U" 

keep pnr y w q age koen d_childl d_childf foed_dagl year month week

********************************************
* Defining ordinary employment (wage), earnings (wage2)
********************************************

capture drop wage
g wage=0
replace wage=1 if w>0 & w!=.
replace wage=0 if y==651 | y==652 /*Study grant*/
replace wage=0 if y==135 | y==136 | y==145 | y==146  ///
                | y==215 | y==216                    ///
				| y==705 | y==706 | y==715 | y==716  ///
				| y==725 | y==726 | y==735 | y==736  ///
				| y==745 | y==746 | y==755 | y==756 | y==765 | y==766 | y==815 | y==816 | y==875 | y==876  ///
				| y==895 | y==896 /*Wage subsidy program*/  ///
				| y==997          /*Deceaced*/ 


capture drop wage2				
g wage2=0
replace wage2=wage*q

********************************************
* Defining parents cohabiting with a child.
* Keeping non-parents and parents cohabiting with a child.
* Save parents_3_`year'.
********************************************
			  
g cohab=.

forvalues year=2000/2006 {
capture drop _merge
merge m:1 pnr year using "$temp/bef_far`year'", keep(master match)
replace cohab=cohab_far`year' if year==`year' & koen==1
capture drop _merge
merge m:1 pnr year using "$temp/bef_mor`year'", keep(master match)
replace cohab=cohab_mor`year' if year==`year' & koen==2
capture drop _merge
}

replace cohab=0 if cohab==.
drop cohab_*

g parent=.
replace parent=1 if d_childl==1 & d_childf==1 & cohab>=1 
replace parent=0 if d_childl==0 & d_childf==0
drop if parent==.


save "$temp/parents_3_`year'", replace
}

***************************************************
*Collect yearly dataset to one.
*Save parents.
***************************************************


use "$temp/parents_3_2000", clear
forvalues year=2001/2006 {
append using "$temp/parents_3_`year'"
}
save "$workdata/parents", replace